package com.freeseed.model.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.freeseed.model.entity.Role;
import com.freeseed.model.mapper.RoleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Allwayz
 * @since 2020-03-20
 */
@Slf4j
@RestController
@RequestMapping("roles")
public class RoleController {
    @Autowired
    private RoleMapper roleMapper;

    @GetMapping
    public List<Role> getRoleList(){
        List<Role> roleList = roleMapper.selectList(new QueryWrapper<>());
        return roleList;
    }

    @GetMapping("{Id}")
    public Role getRoleById(@PathVariable int Id){
        Role role = roleMapper.selectById(Id);
        return role;
    }

    @PostMapping
    public Role addRole(Role role){
        roleMapper.insert(role);
        return role;
    }

    @PutMapping
    public Role updateRole(Role role){
        roleMapper.update(role,new UpdateWrapper<Role>().eq("role_id",role.getRoleId()));
        return role;
    }

    @DeleteMapping("{Id}")
    public void deleteRoleById(@PathVariable int Id){
        roleMapper.deleteById(Id);
    }
}
